package ks.client.gui;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JTextArea;
import javax.swing.JTextField;

import ks.client.controllers.ProcessPchatInputController;
import ks.client.controllers.game.ProcessTableInputController;
import ks.client.lobby.Lobby;

public class UMGUI3CHAT extends javax.swing.JFrame {
	Lobby lobby;

    /** Creates new form UMGUI3CHAT */
    public UMGUI3CHAT(Lobby lobby) {
    	this.lobby = lobby;
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jTextField1 = getChatInput();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = getChatOutput();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTextField1.setFont(new java.awt.Font("Lucida Console", 0, 11)); // NOI18N

        jTextArea1.setColumns(20);
        jTextArea1.setFont(new java.awt.Font("Lucida Console", 0, 13)); // NOI18N
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 254, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 26, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>
    
    JTextField getChatInput() {
		if (jTextField1 == null) {
			jTextField1 = new JTextField(36);
			//jTextField1.setBounds(11, 650, 581, 24);
			jTextField1.setPreferredSize(new Dimension(50, 50));
			
			jTextField1.addActionListener(new ActionListener() {

				public void actionPerformed(ActionEvent e) {
					String s = jTextField1.getText();
					jTextField1.setText("");

					// hand off to the appropriate controller.
					new ProcessPchatInputController(lobby, lobby.getUserContext()).process(s);
				}
				
			});
		}
		return jTextField1;
	}
    
    public JTextArea getChatOutput() {
		if (jTextArea1 == null) {
			jTextArea1 = new JTextArea();
			jTextArea1.setColumns(20);
	        jTextArea1.setRows(5);
		}
		return jTextArea1;
	}
/**
 * Append text to the lobby.
 * 
 * @param s
 */
public void append (String s) {
	String t = jTextArea1.getText() + "\n" + s;
	jTextArea1.setText(t);
	scrollToEnd();		
}
/**
 * Make sure we can scroll to the end on demand.
 */
void scrollToEnd() {
	jTextArea1.setCaretPosition(jTextArea1.getDocument().getLength());
}

    /**
    * @param args the command line arguments
    */

    // Variables declaration - do not modify
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration

}
